<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/8/1  9:26
// +----------------------------------------------------------------------
namespace Service;
//通用类方法
class GeneralService {

    function __construct($container) {
        $this->di = $container;
        //上传或下载路径
        $this->uploadPath='./Upload/wxpic';
    }

    //浏览器直接输出下载
    function outBrowser($filepath , $outfile = 'output.txt') {
        if (!file_exists($filepath)) { //检查文件是否存在
            echo "文件找不到";
            exit;
        } else {
            $file = fopen($filepath , "r"); // 打开文件
            // 输入文件标签

            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
            header("Content-Type:application/force-download");
            Header("Content-type: application/octet-stream");
            Header("Accept-Ranges: bytes");
            Header("Accept-Length: " . filesize($filepath));
            header("Content-Type:application/download");
            Header("Content-Disposition: attachment; filename=" . $outfile);
            header("Content-Transfer-Encoding:binary");
            // 输出文件内容
            echo fread($file , filesize($filepath));
            fclose($file);
            exit;
        }
    }
    //自定义-产生随机数
    function getRandom($len='10') {
        return time();
    }
    //发送post请求
    function postSend($url,$data){
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/x-www-form-urlencoded',
                'content' => $data,
                'timeout' => 15 * 60 // 超时时间（单位:s）
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;
    }
    //发送get请求
    function getSend($url,$data){
        $options = array(
            'http' => array(
                'method' => 'get',
                'header' => 'Content-type:application/x-www-form-urlencoded',
                'content' => $data,
                'timeout' => 15 * 60 // 超时时间（单位:s）
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;
    }
    //从url中下载图片并保存
    function downloadImageFromUrl($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_NOBODY, 0);    //只取body头
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $package = curl_exec($ch);
        $httpinfo = curl_getinfo($ch);

        curl_close($ch);
        $imageAll = array_merge(array('imgBody' => $package), $httpinfo);
        //图片保存路径
        $filename=$this->uploadPath.'/wxpic_'.$this->getRandom().'.png';
        $this->writeFile($filename,$imageAll["imgBody"]);
        //返回保存路径
        return $filename;
    }
    //写入文件
    //参数：文件路径，写入内容
    function writeFile($filename,$content){
        $local_file = fopen($filename, 'w');
        if (false !== $local_file) {
            if (FALSE !== fwrite($local_file , $content)) {
                fclose($local_file);
            }
        }
        return $filename;
    }
}